package com.you.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.you.common.QueryPageParam;
import com.you.common.Result;
import com.you.entity.Goods;
import com.you.entity.Record;
import com.you.service.impl.GoodsServiceImpl;
import com.you.service.impl.RecordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 友友
 * @since 2022-10-25
 */
@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    RecordServiceImpl recordService;
    @Autowired
    GoodsServiceImpl goodsService;

/*


    //修改
    @PostMapping("/mod")
    public Result update(@RequestBody Goods goods) {
        return recordService.updateById(goods)? Result.suc():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam Integer id) {
        return recordService.removeById(id)? Result.suc():Result.fail();
    }
*/


    //自定义分页(查询)
    @PostMapping("/diyListPage")
    public Result diyListPage(@RequestBody QueryPageParam pageParam) {

        HashMap param = pageParam.getParam();
        String goodsName = (String) param.get("name");
        String goodsStorage = (String) param.get("storage");
        String goodsType = (String) param.get("goodstype");
        String roleId = (String) param.get("roleId");
        String userId = (String) param.get("userId");

        Page<Record> page = new Page<>();
        page.setCurrent(pageParam.getPageNum());
        page.setSize(pageParam.getPageSize());

        QueryWrapper<Record> wrapper = new QueryWrapper<>();

        //xml文件中的 where语句写到这里
        wrapper.apply(" record.goods = goods.id and goods.storage = storage.id and goodsType.id = goods.goodsType ");

        if ("2".equals(roleId)){
            wrapper.apply("record.user_id = " + userId);
        }

        if (StringUtils.isNotBlank(goodsName)){

            wrapper.like("goods.name",goodsName);
        }
        if (StringUtils.isNotBlank(goodsStorage)){
            wrapper.eq("storage.id",goodsStorage);
        }
        if (StringUtils.isNotBlank(goodsType)){
            wrapper.eq("goodstype.id", goodsType);
        }

        IPage result = recordService.pageDiy(page, wrapper);

        return Result.suc(result.getRecords(),result.getTotal());

    }

    //(入库and出库)
    @PostMapping("/inOrOutGoods")
    public Result save(@RequestBody Record record) {

        Goods goodsId = goodsService.getById(record.getGoods());
        //出入库数量
        Integer addCount = record.getCount();

        /*出库*/
        if ("outStorage".equals(record.getStatus())){
            addCount = -addCount;
            record.setCount(addCount);
        }

        //现在总数
        Integer newCount = goodsId.getCount() + addCount;
        //赋值给GoodsCount
        goodsId.setCount(newCount);

        goodsService.updateById(goodsId);

        return recordService.save(record)? Result.suc():Result.fail();
    }



}


